﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataSyn.MDL
{
    /// <summary>
    /// 同步管理
    /// </summary>
    class SyncManager
    {
        private List<DataBase.Sync> list_Sync = new List<DataBase.Sync>();
        public DataBase.Sync GetOrCreateProject(ConfigSet.ProjectSection project, bool isCreate = true)
        {
            var back = list_Sync.FirstOrDefault(c => c.projectName == project.ProjectName);
            if (back == null && isCreate)
                list_Sync.Add(back = new DataBase.Sync(project));
            if (back!=null && back.statu() == System.Threading.ThreadState.Unstarted && ClassLibrary_Public.CConver.getBool(project.IsRunning))
                back.StartSync();
            return back;
        }
        public void RemoveProject(ConfigSet.ProjectSection project)
        {
            if (project == null) return;
            var back = GetOrCreateProject(project, false);
            if (back != null)
            {
                back.StopSync();
                list_Sync.Remove(back);
            }
        }
        public void ClosedAll()
        {
            foreach (var sync in list_Sync)
            {
                sync.StopSync();
            }
        }
    }
}
